package com.inkyi.assistant.app.notepad.event;

import com.inkyi.assistant.app.component.rsyntax.RSyntaxTextEditor;
import com.inkyi.assistant.app.notepad.NoteContext;
import com.inkyi.assistant.app.notepad.entity.NoteBaseData;
import com.inkyi.framework.common.event.InkActionEvent;

import java.util.Objects;

/**
 * 记事本 - 工作区的监听事件
 *
 * @author lihongkun
 * @date 2025/08/06
 */
public class NoteWorkspaceListener implements NoteEventListener {


    @Override
    public void actionPerformed(InkActionEvent event) {
        String type = event.getType();
        NoteBaseData data = (NoteBaseData) event.getData();
        System.out.println(type + "-" + data);

        if (Objects.equals(NoteEvent.TYPE_OPEN_NODE, type)) {
            if (NoteContext.getNoteContentPanel().existTabComp(data.getId())) {
                NoteContext.getNoteContentPanel().openTabComp(data.getId());
            } else {
                RSyntaxTextEditor editor = NoteContext.createRSyntaxTextEditor(data);
                if (editor != null) {
                    NoteContext.getNoteContentPanel().addTabComp(data.getId(), data.getTitle(), editor);
                }
            }
        } else if (Objects.equals(NoteEvent.TYPE_UPDATE_TITLE, type)) {
            if (NoteContext.getNoteContentPanel().existTabComp(data.getId())) {
                NoteContext.getNoteContentPanel().setTabCompTitle(data.getId(), data.getTitle());
            }
        } else if (Objects.equals(NoteEvent.TYPE_NEW_NODE, type)) {
            RSyntaxTextEditor editor = NoteContext.createRSyntaxTextEditor(data);
            if (editor != null) {
                NoteContext.getNoteContentPanel().addTabComp(data.getId(), data.getTitle(), editor);
            }
        } else if (Objects.equals(NoteEvent.TYPE_DEL_NODE, type)) {
            NoteContext.getNoteContentPanel().removeTabComp(data.getId());
        }
    }
}
